package gatperdut.chato.core.interpreter.actions;

import gatperdut.chato.constants.CSConstantsCommandResult.CommandExecutionResult;
import gatperdut.chato.core.characters.BaseController;
import gatperdut.chato.core.interpreter.actions.generic.Action;
import gatperdut.chato.core.interpreter.grammar.comminfo.SwitchInfo;
import gatperdut.chato.core.item.ItemController;
import gatperdut.chato.model.Chato.Constant.WearLoc;

public class SwitchAction extends Action {

    public SwitchAction( SwitchInfo info, BaseController entityController ) {
        super( info, entityController );
    }

    @Override
    public CommandExecutionResult perform() {
        checkForRunningCraft();

        if ( emptyEmote() ) {
            getEntityController().tx( "Empty emotes are not allowed." );
            return CommandExecutionResult.OK;
        }

        String emote = getLastPartEmote();
        if ( emote == null ) {
            getEntityController().tx( "Check your emote." );
            return CommandExecutionResult.OK;
        }

        ItemController l = getEntityController().getLHandItemController();
        ItemController r = getEntityController().getRHandItemController();

        if ( l == null && r == null ) {
            getEntityController().tx( "You are not holding anything." );
            return CommandExecutionResult.OK;
        }

        if ( l != null && r != null ) {
            getEntityController().getInventoryController().takeFromInventory( l );
            getEntityController().getInventoryController().takeFromInventory( r );
            getEntityController().getInventoryController().putInInventory( WearLoc.RHAND, l );
            getEntityController().getInventoryController().putInInventory( WearLoc.LHAND, r );
            getEntityController().getEchoes().txSwitchBothHands( l, r, emote );
        }
        else if ( l != null ) {
            getEntityController().getInventoryController().takeFromInventory( l );
            getEntityController().getInventoryController().putInInventory( WearLoc.RHAND, l );
            getEntityController().getEchoes().txSwitchOneHand( l, WearLoc.RHAND, emote );
        }
        else if ( r != null ) {
            getEntityController().getInventoryController().takeFromInventory( r );
            getEntityController().getInventoryController().putInInventory( WearLoc.LHAND, r );
            getEntityController().getEchoes().txSwitchOneHand( r, WearLoc.LHAND, emote );
        }

        return CommandExecutionResult.OK;
    }

    @Override
    public SwitchInfo getInfo() {
        return (SwitchInfo) super.getInfo();
    }

    @Override
    public boolean interruptsCrafting() {
        return false;
    }

}
